<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
require_once("../conn.php");
require_once 'common.php';

$price = checkTrim($_POST['price']);
$goodprice = checkTrim($_POST['goodprice']);
$iconurl = checkTrim($_POST['iconurl']);
$newIconUrl = checkTrim($_POST['newIconUrl']);
$qsturl = checkTrim($_POST['qsturl']);
$musicurl = checkTrim($_POST['musicurl']);
$imageurl = checkTrim($_POST['imageurl']);
$code = checkTrim($_POST['code']);
$version = checkTrim($_POST['version']);
$iconVersion = checkTrim($_POST['iconVersion']);
$appleId = getParameter("appleId");
$name = checkTrim($_POST['name']);
$os = checkTrim($_POST['os']);
$title = getParameter("title");
$gold = getParameter("gold");
$dollar = getParameter("dollar");
$dollarID = getParameter("dollarID");
$isModify = $_POST['isModify'];
$promotion = "0";
if (isset($_POST['promotion'])) {
    $promotion = checkTrim($_POST['promotion']);
    if ($promotion == "on") {
        $promotion = "1";
    }
}
$db = new DBC();
$is_new = true;
$is_code_exist = false;
if ($isModify == 1) {
    if ($gold == "") {
        $gold = 0;
    }
    $sql = "UPDATE t_hibaby_title_packet SET promotion = '" . $promotion . "',price = '" . $price . "',goodprice = '" . $goodprice . "',iconUrl = '" . $iconurl . "',newIconUrl = '" . $newIconUrl . "',qstUrl = '" . $qsturl . "',os = '" . $os . "',musicUrl = '" . $musicurl . "',imageUrl = '" . $imageurl . "',title = '" . $title . "',gold = " . $gold . ",dollar = '" . $dollar . "',name = '" . $name . "',version = '" . $version . "',iconVersion = '" . $iconVersion . "',appleId = '" . $appleId . "',dollarID = '" . $dollarID . "' WHERE code = '" . $code . "'";
} else {
    $sql = "SELECT id FROM t_hibaby_titlePacket WHERE code = '" . $code . "'";
    $data = $db->fetchData($sql);
    if ($data != null && count($data) > 0) {
        $is_code_exist = true;
    }
    if ($gold == "") {
        $gold = 0;
    }

    $sql = "INSERT INTO t_hibaby_title_packet(code,price,goodprice,promotion,iconUrl,newIconUrl,qstUrl,musicUrl,imageUrl,name,version,iconVersion,appleId,os,createTime,title,dollar,gold,dollarID) VALUES('" . $code . "','" . $price . "','" . $goodprice . "','" . $promotion . "','" . $iconurl . "','" . $newIconUrl . "','" . $qsturl . "','" . $musicurl . "','" . $imageurl . "','" . $name . "','" . $version . "','" . $iconVersion . "','" . $appleId . "','" . $os . "',now(),'" . $title . "','" . $dollar . "'," . $gold . "," . $dollarID . ")";
    $is_new = true;
}
if ($is_code_exist) {
    $msg = "编号 " . $code . " 已经存在。";
    echo "<script language='javascript'>alert('" . $msg . "'); </script>";
    echo "<script language='javascript'>history.back();</script>";
} else {
    $transfer = "titlePacketList.php?menu=titlePacket";
    if ($db->execute($sql)) {
        if ($is_new) {
            $sql = "CREATE TABLE t_a_game_" . $code . "(
            `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
            `code` varchar(50) DEFAULT NULL COMMENT '编码',
            `name` varchar(200) DEFAULT NULL COMMENT '题目',
            `level1` varchar(50) DEFAULT NULL COMMENT '第一级分类',
            `level2` varchar(50) DEFAULT NULL COMMENT '第二级分类',
            `level3` varchar(50) DEFAULT NULL COMMENT '第三级分类',
            `answer` varchar(50) DEFAULT NULL,
            `answer1` varchar(50) DEFAULT NULL COMMENT '正确答案 如A001-A001',
            `answer2` varchar(50) DEFAULT NULL COMMENT '错误答案 如A005-A009',
            `answer3` varchar(50) DEFAULT NULL COMMENT '错误答案 如A015-A019',
            `answer4` varchar(50) DEFAULT NULL COMMENT '错误答案 如A025-A029',
            `answer5` varchar(50) DEFAULT NULL,
            `voice1` varchar(50) DEFAULT NULL COMMENT '语音ID 1',
            `voice2` varchar(50) DEFAULT NULL COMMENT '语音ID 2',
            `pic` varchar(50) DEFAULT NULL COMMENT '图片ID',
            `difficult` varchar(50) DEFAULT NULL COMMENT '难度',
            `url` varchar(200) DEFAULT NULL COMMENT '文件地址',
            `status` int(11) DEFAULT '0' COMMENT '0 未发布 1 已发布',
            `version` varchar(50) DEFAULT NULL,
            `updateTime` datetime DEFAULT NULL,
            `createTime` datetime DEFAULT NULL,
            PRIMARY KEY (`id`)
          ) ENGINE=MyISAM AUTO_INCREMENT=0 DEFAULT CHARSET=utf8";
            $db->execute($sql);
            //success
            echo "<script language='javascript'>location='" . $transfer . "';</script>";
        } else {
            //failue
            echo "<script language='javascript'>alert('失败'); </script>";
            echo "<script language='javascript'>history.back();</script>";
        }
    }
}

function checkTrim($item) {
    if (isset($item)) {
        $item = trim($item);
    } else {
        $item = "";
    }
    return $item;
}
?>
